let app = new Vue({
    el: "#app",
    data() {
        return {
			uploadurl:axios.defaults.baseURL + "api/uploadframe/upload",
			headers: {
				Token: localStorage.getItem('TOKEN'),
				UserID: localStorage.getItem('USERID')
			},
			rootdir:"",
			toolbar:[
				{content:"创建目录", icon:"el-icon-document-add",type:"primary"},
			],
			tablecolums:[
				{prop:"type", label:"类别",width:"100"},
				{prop:"name", label:"名称",width:"300"},
				{prop:"count", label:"包含",width:"300"},
			],
			dialogFormVisible:false,
			queryForm:{
                curdir: "",
				type:"目录",
				root:""
			},
			uploadframeRules:{
			    name: [
			        {required: true, message: '请输入目录名', trigger: 'blur'}
			    ]
			}, // 数据验证规则
			uploadframeForm: {
                name: "",
				curdir:"",
				type: ""
            },
			pagedata:[{
                name: "法人身份证",
				type:"目录"
			}],
			loaded:false,
        }
    },
	methods: {
		toolAction(acName) {
			if (acName == "创建目录") {
				this.add()
			} 
		},
		// 确认
		async cofirm(){
            this.$refs.uploadframeForm.validate(async valid => {
                if (valid){
					// 显示加载框
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res = null;
						if (this.action=="add") {
							res = await axios.put('api/uploadframe/mkdir', this.uploadframeForm);
						} else{
							// 编辑
							res = await axios.put('api/uploadframe/update', this.uploadframeForm);
						}
						if (res.code === 200) {
							this.query();
							this.dialogFormVisible = false
						}else {
							this.$message.error(res.message);
						}		
					}catch (e) {
						this.$message.error('添加或编辑请求出错！', e.message);
					}
					// 关闭加载框
					loading.close();
                }
            })			
		},
		// 查询
		async query() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.post('api/uploadframe/query', this.queryForm);
				if (res.code === 200) {
					this.pagedata = res.data;
					this.$message.success(res.message);
				}else {
					this.$message.error(res.message);
				}
			}catch (e) {
				this.$message.error('请求出错！', e.message);
			}
			
			// 关闭加载框
			loading.close();
		},
		// 添加
		add() {
			this.action="add";
			this.dialogFormVisible = true;
		    this.uploadframeForm= {
		        name: "",
				curdir:this.queryForm.curdir,
		    	type: "目录",
		    }
		},
		// 编辑
		edit(index, row) {
			this.action="edit";
			this.index = index;
			this.dialogFormVisible = true;
			this.uploadframeForm = deepCopy(row)
		},
		// 删除
		async del(index, row) {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				this.$confirm('确认要删除当前目录吗？是否继续?', '提示', {
 			                confirmButtonText: '确定',
 			                cancelButtonText: '取消',
 			                type: 'warning'
	            }).then(async () => {
					this.uploadframeForm={
		                name: row.name,
						curdir:this.queryForm.curdir,
						type: row.type
		            }
					let res = await axios.put('api/uploadframe/rmtree', this.uploadframeForm);
					if (res.code === 200) {
						this.query()
						this.$message.success(res.message);
					}else {
						this.$message.error(res.message);
					}
	            }).catch((e) => {
	                if (e.message) this.Msg('error', e.message);
	            });
			}catch (e) {
				this.$message.error('删除出错！', e.message);
			}
			
			// 关闭加载框
			loading.close();
		},
		// 上层目录
		up() {
			index =this.queryForm.curdir.lastIndexOf("/")
			this.queryForm.curdir = this.queryForm.curdir.substring(0,index)
			
			this.query()
		},
		// 进入目录
		async into(row, column, event) {
			if (row.type == "目录") {
				this.queryForm.curdir =this.queryForm.curdir +"/"+ row.name
				this.queryForm.root = this.rootdir
				this.queryForm.type = row.type
				this.query()
			} else {
					this.uploadframeForm={
		                name: row.name,
						curdir:this.queryForm.curdir,
						type: row.type
		            }
					let res = await axios.post('api/uploadframe/download', this.uploadframeForm,'blob');
					 if(res){
					  const blData = res //拿到blob数据
					  const urlObjData = window.URL.createObjectURL(new Blob([res]))
					  //获取文件 进行下转码	  
					  const fileName = row.name
					   
					  //创建a标签 点击a标签 达到下载目的
					  const link = document.createElement('a')
					  link.href = urlObjData
					  link.download = fileName //下载文件的名字
					  document.body.appendChild(link)
					  link.click()
					   
					  document.body.removeChild(link)
					  window.URL.revokeObjectURL(urlObjData);
					   
					 } else {
						this.$message.error("下载失败！");
					}
			}
		},
		// 上传成功
		handleAvatarSuccess(res, file, filelist) {
			this.query();
		}
	},
	async mounted() {
		let param=window.location.href.split('=')
		let docnumber=param[param.length-1]
		this.rootdir='客户档案/' + docnumber
		this.queryForm.curdir = this.rootdir
		this.queryForm.root = this.rootdir
		this.queryForm.type = "目录"

		this.query()
	}

});	